﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;

namespace ApplicationSettingsManager
{
	public class Manager
	{
		public ApplicationSettings AppSettings = new ApplicationSettings();
		public TableStorageSettings TableSettings = new TableStorageSettings();
		public const string SettingsFileName = "ApplicationSettings.xml";

		public Manager()
		{
			if(File.Exists(SettingsFileName))
			{
				ReadApplicationSettingFile(SettingsFileName, "applicationSettings");
			}
			else
			{
				GetAppConfigFileSettings();
			}

			// Next get the table storage settings that will be used by the applications
			GetTableStorageSettings();
		}

		/// <summary>
		/// Reads the specified XML file name.
		/// </summary>
		/// <param name="fileName">Name of the file.</param>
		/// <param name="xmlElementTag">The XML element tag.</param>
		private void ReadApplicationSettingFile(string fileName, string xmlElementTag)
		{
			try
			{
				XDocument xmlDoc = XDocument.Load(fileName);		// loads the xmldata
				//load the xml file and selects from the elements collection the specified
				//into an array of elements that are children of xmlElementTag
				var settings = (from c in xmlDoc.Elements(xmlElementTag)
								select c).Elements().ToArray();

				foreach (var setting in settings)
				{
					Trace.WriteLine(setting.Name + " " + setting.Value);
					AppSettings.SetValue(setting.Name.ToString(), setting.Value);
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine("Error: " + ex.Message);
			}

		}
		
		/// <summary>
		/// Gets the app.config file settings.
		/// </summary>
		private void GetAppConfigFileSettings()
		{
			var subscriptionID = Properties.Settings.Default.subscriptionID;

			AppSettings = new ApplicationSettings
			{
				AzureDataConnectionString = Properties.Settings.Default.AzureDataConnectionString,
				ConfigurationDALTableName = Properties.Settings.Default.ConfigurationDALTableName,
				ConfigurationPartitionKey = Properties.Settings.Default.ConfigurationPartitionKey,
				ConfigurationRoleRowKey = String.IsNullOrEmpty(Properties.Settings.Default.ConfigurationRoleRowKey) ? subscriptionID : Properties.Settings.Default.ConfigurationRoleRowKey,
				CounterSettingsTableName = Properties.Settings.Default.CounterSettingsTableName,
				ControlledRoleName = Properties.Settings.Default.ControlledRoleName,
				PerformanceCountersConnectionString = Properties.Settings.Default.PerformanceCountersConnectionString,
				PerformanceCountersPartitionKey = String.IsNullOrEmpty(Properties.Settings.Default.PerformanceCountersPartitionKey) ? subscriptionID : Properties.Settings.Default.PerformanceCountersPartitionKey,
				StateMachineConnectionString = Properties.Settings.Default.StateMachineConnectionString,
				StateMachinePartitionKey = String.IsNullOrEmpty(Properties.Settings.Default.StateMachinePartitionKey) ? subscriptionID : Properties.Settings.Default.StateMachinePartitionKey,
				StateMachineTableName = Properties.Settings.Default.StateMachineTableName,
				WADiagnosticConnectionString = Properties.Settings.Default.WADiagnosticConnectionString,
				WADPerformanceCountersDALTableName = Properties.Settings.Default.WADPerformanceCountersDALTableName,
				SubscriptionID = subscriptionID,
				ThumbPrint = Properties.Settings.Default.thumbprint,
				MaxInstances = 15,
				MinInstances = 5
			};

		}

		private void GetTableStorageSettings()
		{

		}


	}
}
